package cn.cumulomq.dao;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import cn.cumulomq.entity.TeleMessage;

public interface TeleMessageDao {
	@Insert("insert into telemessage(id,ADDITIONAL_SERVICE,CID,CSN,ORIGINAL_TEXT,PARSE_ERROR,RECEIVED_DATE,SENT_DATE,ADDRESSEES,CREATE_TIME,DOUBLE_SIGNATURE,FILING_TIME,MARK_VIEWED,MESSAGE_IDENTITY,MSG_DIRECTION,MSG_TYPE,OPTIONAL_HEADING,ORIGINATOR,PRIORITY_ALARM,PRIORITY_INDICATOR,TEXT,USERNAME,MSG_CATEGORY,parseJson) values(#{id},#{additionalService},#{cid},#{csn},#{originalText},#{parseError},#{receivedDate},#{sentDate},#{addressees},#{createTime},#{doubleSignature},#{filingTime},#{markViewed},#{messageIdentity},#{msgDirection},#{msgType},#{optionalHeading},#{originator},#{priorityAlarm},#{priorityIndicator},#{text},#{userName},#{msgCategory},#{parseJson})")
	public int insert(TeleMessage teleMessage);
	@Update("update telemessage set parseJson=#{parseJson},ADDITIONAL_SERVICE = #{additionalService},CID=#{cid},CSN = #{csn},ORIGINAL_TEXT = #{originalText},PARSE_ERROR = #{parseError},RECEIVED_DATE = #{receivedDate},SENT_DATE = #{sentDate},ADDRESSEES = #{addressees},CREATE_TIME = #{createTime},DOUBLE_SIGNATURE = #{doubleSignature},FILING_TIME = #{filingTime},MARK_VIEWED = #{markViewed},MESSAGE_IDENTITY = #{messageIdentity},MSG_DIRECTION = #{msgDirection},MSG_TYPE = #{msgType},OPTIONAL_HEADING = #{optionalHeading},ORIGINATOR = #{originator},PRIORITY_ALARM = #{priorityAlarm},PRIORITY_INDICATOR = #{priorityIndicator},TEXT = #{text},USERNAME = #{userName},MSG_CATEGORY= #{msgCategory} where id=#{id}")
	public int update(TeleMessage teleMessage);
	@Delete("delete telemessage where id=#{id}")
	public int delete(String id);
	@Select("select id as id,parseJson as parseJson,ADDITIONAL_SERVICE  as additionalService,CID as cid,CSN  as csn,ORIGINAL_TEXT  as originalText,PARSE_ERROR  as parseError,RECEIVED_DATE  as receivedDate,SENT_DATE  as sentDate,ADDRESSEES  as addressees,CREATE_TIME  as createTime,DOUBLE_SIGNATURE  as doubleSignature,FILING_TIME  as filingTime,MARK_VIEWED  as markViewed,MESSAGE_IDENTITY  as messageIdentity,MSG_DIRECTION  as msgDirection,MSG_TYPE  as msgType,OPTIONAL_HEADING  as optionalHeading,ORIGINATOR  as originator,PRIORITY_ALARM  as priorityAlarm,PRIORITY_INDICATOR  as priorityIndicator,TEXT  as text,USERNAME  as userName,MSG_CATEGORY as msgCategory  from telemessage where id=#{id}")
	public TeleMessage get(String id);
	@Select("<script>select id as id,parseJson as parseJson,ADDITIONAL_SERVICE  as additionalService,CID as cid,CSN  as csn,ORIGINAL_TEXT  as originalText,PARSE_ERROR  as parseError,RECEIVED_DATE  as receivedDate,SENT_DATE  as sentDate,ADDRESSEES  as addressees,CREATE_TIME  as createTime,DOUBLE_SIGNATURE  as doubleSignature,FILING_TIME  as filingTime,MARK_VIEWED  as markViewed,MESSAGE_IDENTITY  as messageIdentity,MSG_DIRECTION  as msgDirection,MSG_TYPE  as msgType,OPTIONAL_HEADING  as optionalHeading,ORIGINATOR  as originator,PRIORITY_ALARM  as priorityAlarm,PRIORITY_INDICATOR  as priorityIndicator,TEXT  as text,USERNAME  as userName,MSG_CATEGORY as msgCategory  from telemessage where MSG_DIRECTION=#{teleMsg.msgDirection} "
			+"<when test='teleMsg.text!=null'> and text like '%'||#{teleMsg.text}||'%' </when>"
			+"<when test='teleMsg.originator!=null'> and originator = #{teleMsg.originator} </when>"
			+"<when test='teleMsg.msgDirection ==0 and teleMsg.queryStartDate!=null'> and received_Date &gt;= #{teleMsg.queryStartDate} </when>"
			+"<when test='teleMsg.msgDirection ==0 and teleMsg.queryEndDate!=null'> and received_Date &lt;= #{teleMsg.queryEndDate}  </when>"
			+"<when test='teleMsg.msgDirection ==1 and teleMsg.queryStartDate!=null'> and sent_Date &gt;= #{teleMsg.queryStartDate} </when>"
			+"<when test='teleMsg.msgDirection ==1 and teleMsg.queryEndDate!=null'> and sent_Date &lt;= #{teleMsg.queryEndDate} </when>"
			+" order by CREATE_TIME desc limit #{1} offset #{1}*(#{2}-1)</script>")
	public List<TeleMessage> query(@Param("teleMsg")TeleMessage teleMessage,int pagesize,int pageno);
	@Select("<script>select count(id) as cnt from telemessage where MSG_DIRECTION=#{teleMsg.msgDirection}"
			+"<when test='teleMsg.text!=null'> and text like '%'||#{teleMsg.text}||'%' </when>"
			+"<when test='teleMsg.originator!=null'> and originator = #{teleMsg.originator} </when>"
			+"<when test='teleMsg.msgDirection ==0 and teleMsg.queryStartDate!=null'> and received_Date &gt;= #{teleMsg.queryStartDate} </when>"
			+"<when test='teleMsg.msgDirection ==0 and teleMsg.queryEndDate!=null'> and received_Date &lt;= #{teleMsg.queryEndDate}  </when>"
			+"<when test='teleMsg.msgDirection ==1 and teleMsg.queryStartDate!=null'> and sent_Date &gt;= #{teleMsg.queryStartDate} </when>"
			+"<when test='teleMsg.msgDirection ==1 and teleMsg.queryEndDate!=null'> and sent_Date &lt;= #{teleMsg.queryEndDate} </when>"
			+ "</script>")
	public int count(@Param("teleMsg")TeleMessage teleMessage);
}
